package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌(和Javaweb一致)
        Map<String, Object> claims = new HashMap<>();
        //我们这里的自定义数据设置了id 我们才能用AOP+ThreadLocal 才能获取id
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        //这里我们使用工具类的方法创建我们的JWT令牌(属性在配置文件里面)
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        //这里我们按照接口文档 按VO类型返回(用构建器链式编程类型创建对象)
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     * @param employeeDTO
     * @return
     */
    @PostMapping//根据接口文档这里没有别的路径
    @ApiOperation("新增员工")//swagger的提示文字
    public Result addEmployee(@RequestBody EmployeeDTO employeeDTO) {//注意如果请求参数是JSON我们用什么注解来接受
        log.info("新增员工{}", employeeDTO);//记住要打日志！
        employeeService.add(employeeDTO);
        return Result.success();//这里按照接口文档返回一个code为1的result
    }

    /**
     *分页查询
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> limitserach(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("分页查询{}", employeePageQueryDTO);
        //这里我们把接收请求的DTO传入Service层中(了解为什么有的药转换entity)
        PageResult page=employeeService.page(employeePageQueryDTO);
        return Result.success(page);
    }

    /**
     * 员工状态设置
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("员工状态设置")
    public Result updateStatus(@PathVariable Integer status,long id) {
        log.info("员工状态设置{}{}", id,status);
        employeeService.updatestatus(status,id);
        return Result.success();
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result<Employee> getbyid(@PathVariable long id) {
        log.info("根据id查询员工{}",id);
       Employee employee= employeeService.search(id);
        return Result.success(employee);
    }


    @PutMapping
    @ApiOperation("编辑员工")
    //注意我们使用DTO来接收参数
    public Result update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("编辑员工信息{}", employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
